package com.atldy.commerce.config.feign;

import feign.Feign;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * @version 1.0
 * @Author: li-dong-yang
 * @CreateTime: 2021-08-25 7:30
 * @Description： okhttp 配置
 */
@Configuration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class FeignHttpConfig {

    /**
     * 配置okhttp链接
     */
    @Bean
    public OkHttpClient okHttpClient() {
        return new OkHttpClient.Builder()
                //设置链接超时
                .connectTimeout(5, TimeUnit.SECONDS)
                //设置读超时
                .readTimeout(5, TimeUnit.SECONDS)
                //设置写超时
                .writeTimeout(5, TimeUnit.SECONDS)
                //是否自动重连
                .retryOnConnectionFailure(true)
                //配置线程池 最大空闲线程数是10 并保持五分钟
                .connectionPool(new ConnectionPool(
                        10, 5, TimeUnit.MINUTES
                )).build();
    }

}